<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'

const router = useRouter()
const route = useRoute()
const collapsed = ref(false)

// 路由名称映射
const routeNameMap = {
  'setting': '设备配置',
  'shooting': '设备启动',
  'result': '分析结果',
  'softwareInfo': '软件信息'
}

// 当前路由
const currentRoute = computed(() => {
  return route.path.split('/').pop()
})

// 处理菜单折叠
const handleCollapse = (val) => {
  collapsed.value = val
}

// 处理菜单点击
const handleMenuClick = (key) => {
  router.push(`/app/${key}`)
}

// 处理退出登录
const handleLogout = () => {
  router.push('/login')
}

// 添加下拉菜单处理函数
const handleDropdownSelect = (value) => {
  if (value === 'logout') {
    handleLogout()
  }
}
</script>

<template>
  <a-layout class="layout-container">
    <!-- 顶部导航栏 -->
    <a-layout-header class="header">
      <div class="header-left">
        <!-- 使用图标替代logo图片 -->
        <div class="logo-icon">
          <icon-apps />
        </div>
        <h2 class="title">砂级配检测系统</h2>
      </div>
      <div class="header-right">
        <a-space>
          <a-avatar>
            <icon-user />
          </a-avatar>
          <a-dropdown @select="handleDropdownSelect">
            <a-button type="text">
              管理员
              <icon-down />
            </a-button>
            <template #content>
              <a-doption value="profile">
                <template #icon><icon-user /></template>
                个人信息
              </a-doption>
              <a-doption value="logout">
                <template #icon><icon-poweroff /></template>
                退出登录
              </a-doption>
            </template>
          </a-dropdown>
        </a-space>
      </div>
    </a-layout-header>

    <a-layout class="main-container">
      <!-- 左侧菜单 -->
      <a-layout-sider
        breakpoint="lg"
        :collapsed="collapsed"
        :collapsible="true"
        :width="220"
        class="sider"
        @collapse="handleCollapse"
      >
        <a-menu
          :selected-keys="[currentRoute]"
          :style="{ width: '100%' }"
          @menu-item-click="handleMenuClick"
        >
          <a-menu-item key="setting">
            <template #icon><icon-settings /></template>
            配置设备
          </a-menu-item>
          <a-menu-item key="shooting">
            <template #icon><icon-camera /></template>
            设备启动
          </a-menu-item>
          <a-menu-item key="result">
            <template #icon><icon-bar-chart /></template>
            分析结果
          </a-menu-item>
          <a-menu-item key="softwareInfo">
            <template #icon><icon-info-circle /></template>
            软件信息
          </a-menu-item>
        </a-menu>
      </a-layout-sider>

      <!-- 主内容区 -->
      <a-layout-content class="content">
        <a-breadcrumb class="breadcrumb">
          <a-breadcrumb-item>首页</a-breadcrumb-item>
          <a-breadcrumb-item>{{ routeNameMap[currentRoute] }}</a-breadcrumb-item>
        </a-breadcrumb>
        
        <div class="content-wrapper">
          <router-view v-slot="{ Component }">
            <transition name="fade" mode="out-in">
              <component :is="Component" />
            </transition>
          </router-view>
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<style scoped>
.layout-container {
  height: 100vh;
  width: 100vw;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  background: var(--color-bg-2);
  border-bottom: 1px solid var(--color-border);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: var(--color-text-1);
  background: var(--color-fill-3);
  border-radius: 4px;
}

.title {
  margin: 0;
  font-size: 18px;
  color: var(--color-text-1);
}

.main-container {
  height: calc(100vh - 60px);
}

.sider {
  background: var(--color-bg-2);
  border-right: 1px solid var(--color-border);
}

.content {
  padding: 16px;
  background: var(--color-bg-1);
  overflow: auto;
}

.breadcrumb {
  margin-bottom: 16px;
}

.content-wrapper {
  background: var(--color-bg-2);
  padding: 20px;
  border-radius: 4px;
  min-height: calc(100% - 60px);
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 响应式调整 */
@media screen and (max-width: 768px) {
  .header-left .title {
    display: none;
  }
  
  .content {
    padding: 12px;
  }
  
  .content-wrapper {
    padding: 16px;
  }
}
</style>
